# -*- coding: utf-8 -*-
# author: ting.wei
# time: 2022/10/25
import time

from appium.webdriver.common.appiumby import AppiumBy
from appium.webdriver.extensions.android.nativekey import AndroidKey

from proj_spec.CLUE.mobile_po.clue_app_base_page import CLUEAppBasePage
from proj_spec.CLUE.mobile_po.company_detail_page import CompanyDetailPage
from selenium.webdriver.common.keys import Keys


class FollowedCompanyListPage(CLUEAppBasePage):
    """
    企业关注列表页面
    """
    _confirm_btn_locator_android = (AppiumBy.ANDROID_UIAUTOMATOR, 'UiSelector().text("确认")')
    _confirm_btn_locator_ios = (AppiumBy.ACCESSIBILITY_ID, "确认")

    _search_locator_android = (AppiumBy.XPATH, "//*[contains(@text,'pages/control/list')]//android.widget.EditText")
    _search_locator_ios = (AppiumBy.IOS_PREDICATE, 'type == "XCUIElementTypeSearchField"')

    _search_results_count_android = (AppiumBy.XPATH, "//*[@text='为您找到']/following-sibling::*[1]")
    _search_results_count_ios = (AppiumBy.XPATH, '//XCUIElementTypeStaticText[@name="为您找到"]/following-sibling::XCUIElementTypeStaticText[1]')

    def get_company_name_locator(self, company_name):
        """
        获取企业的标题
        """
        if self.os == 'android':
            return (AppiumBy.XPATH, f"//*[contains(@text,'pages/control/list')]//*[@text='{company_name}' and not(contains(@class, 'Edit'))]")
        else:
            # todo: 当前无层级包含关系，只能在搜索后使用
            return (AppiumBy.IOS_PREDICATE, f'label == "{company_name}" and accessible == true')

    def get_company_potential_customer_locator(self, company_name):
        """
        获取指定企业的潜客按钮元素
        """
        if self.os == 'android':
            return (AppiumBy.XPATH, f"//*[contains(@text,'pages/control/list')]//*[@text='{company_name}']/../../*[5]/*[2]")
        else:
            # todo: 当前无层级包含关系，只能在搜索后使用
            return (AppiumBy.IOS_PREDICATE, 'label contains "潜客"')

    def to_com_detail(self, company_name):
        """
        选择企业，进入公司详情页面
        """
        self.find_element_and_click(self.get_company_name_locator(company_name))
        return CompanyDetailPage(self.driver)

    def add_potential_customer(self, company_name):
        """
        添加潜客
        """
        potential_customer_btn = self.find_element(self.get_company_potential_customer_locator(company_name))
        assert potential_customer_btn.get_attribute("name") == '潜客', f'该公司已添加潜客：{company_name}'
        potential_customer_btn.click()
        time.sleep(1)
        # 添加潜客后判断按钮文案是否符合预期
        assert potential_customer_btn.get_attribute("name") == '删潜客', '添加潜客失败'

    def delete_potential_customer(self, company_name):
        """
        删除潜客
        """
        potential_customer_btn = self.find_element(self.get_company_potential_customer_locator(company_name))
        assert potential_customer_btn.get_attribute("name") == '删潜客', f'该公司已删除潜客：{company_name}'
        potential_customer_btn.click()
        self.find_element_and_click(getattr(self, '_confirm_btn_locator_' + self.os))
        time.sleep(1)
        # 删除潜客后判断按钮文案是否符合预期
        assert potential_customer_btn.get_attribute("name") == '潜客', '删除潜客失败'

    def search_company(self, company_name):
        """搜索企业
        return: 搜索结果总数
        """
        search_box = self.find_element(getattr(self, '_search_locator_' + self.os))
        search_box.click()
        search_box.send_keys(company_name)
        if self.os == 'android':
            self.driver.press_keycode(AndroidKey.ENTER)
            return self.find_element(getattr(self, '_search_results_count_' + self.os)).text
        else:
            search_box.send_keys(Keys.ENTER)
            return self.find_element(getattr(self, '_search_results_count_' + self.os)).get_attribute("name")

    def clear_search(self):
        """清空输入框的内容"""
        search_box = self.find_element(getattr(self, '_search_locator_' + self.os))
        search_box.click()
        if self.os == 'android':
            search_box.clear()
            self.driver.press_keycode(AndroidKey.ENTER)
        else:
            # 输入框内的数据过长时，clear无法完全清空，需要重复调用clear
            while search_box.get_attribute("value") is not None and len(search_box.get_attribute("value")) > 0:
                search_box.clear()
            search_box.send_keys(Keys.ENTER)

    def go_back(self):
        """IOS端driver.back()不work，需要使用后退按钮

        :return:
        """
        if self.os == 'android':
            self.driver.back()
        else:
            self.find_element_and_click(getattr(self, '_back_btn_locator_' + self.os))